import router, { constantRoutes } from '@/router'
import { getRouters } from '@/api/menu'
import Layout from '@/layout/index'

// 匹配views里面所有的.vue文件
const modules = import.meta.glob('./../../views/**/*.vue')
const rdata = [{
  path: '/originaldata',
  component: "Layout",
  hidden: false,
  redirect: 'originaldata',
  meta: { title: '原始数据', icon: 'icon001', affix: false, breadcrumb: false },
  children: [
    {
      path: '',
      component: "originalData/index",
      name: 'originaldata',
      meta: { title: '原始数据', icon: 'icon001' }
    }
  ]
}, {
  path: '/visualdata',
  component: "Layout",
  hidden: false,
  redirect: 'visualdata',
  meta: { title: '可视化数据', icon: 'icon002', affix: false, breadcrumb: false },
  children: [
    {
      path: '',
      component: "visualData/index",
      name: 'visualdata',
      meta: { title: '可视化数据', icon: 'icon002' }
    }
  ]
}, {
  path: '/algormodel',
  component: "Layout",
  hidden: false,
  redirect: 'algormodel',
  meta: { title: '算法模型', icon: 'icon003', affix: false, breadcrumb: false },
  children: [
    {
      path: '',
      component: "algorModel/index",
      name: 'algormodel',
      meta: { title: '算法模型', icon: 'icon003' }
    }
  ]
}, {
  path: '/testtask',
  component: "Layout",
  hidden: false,
  redirect: 'testtask',
  meta: { title: '测试任务', icon: 'icon003', affix: false, breadcrumb: false },
  children: [
    {
      path: '',
      component: "testTask/index",
      name: 'testtask',
      meta: { title: '测试任务', icon: 'icon003' }
    },
    {
      path: 'media',
      component: "testTask/media",
      name: 'media',
      hidden:true,
      meta: { title: '数据实时监控', icon: 'icon003'}
    }
  ]
},{
  path: '/database',
  component: "Layout",
  hidden: false,
  redirect: 'database',
  meta: { title: '数据库配置', icon: 'icon003', affix: false, breadcrumb: false },
  children: [
    {
      path: '',
      component: "dataBase/index",
      name: 'database',
      meta: { title: '数据库配置', icon: 'icon003' }
    }
  ]
}
// {
//   path: '/media',
//   component: "Layout",
//   hidden: true,
//   redirect: 'media',
//   meta: { title: '数据实时监控', icon: 'icon003', affix: false, breadcrumb: false },
//   children: [
//     {
//       path: '',
//       component: "media/index",
//       name: 'media',
//       meta: { title: '数据实时监控', icon: 'icon003' }
//     }
//   ]
// }
  //, {
  //   path: '/specsdata',
  //   component: "Layout",
  //   hidden: false,
  //   redirect: 'specsdata',
  //   meta: { title: '数据规范测试', icon: 'icon004', affix: false },
  //   children: [
  //     {
  //       path: '',
  //       component: "specsData/index",
  //       name: 'specsdata',
  //       meta: { title: '数据规范测试', icon: 'icon004' }
  //     }
  //   ]
  // },
  // {
  //   path: '/funcconfig',
  //   component: "Layout",
  //   hidden: false,
  //   redirect: 'funcconfig',
  //   meta: { title: '解析函数配置', icon: 'icon005', affix: false },
  //   children: [
  //     {
  //       path: '',
  //       component: "funcConfig/index",
  //       name: 'funcconfig',
  //       meta: { title: '解析函数配置', icon: 'icon005' }
  //     }
  //   ]
  // }
];
const usePermissionStore = defineStore(
  'permission',
  {
    state: () => ({
      routes: [],
      addRoutes: [],
      sidebarRouters: [
        {
          path: '',
          component: 'Layout',
          redirect: '/index',
          children: [
            {
              path: '/index',
              component: () => import('@/views/index'),
              name: 'Index',
              meta: { title: '首页', icon: 'dashboard', affix: true }
            }
          ]
        },
      ]
    }),
    actions: {
      setSidebarRouters(routes) {
        this.sidebarRouters = routes
      },
      generateRoutes() {
        return new Promise(resolve => {
          // 向后端请求路由数据
          // const sdata = JSON.parse(JSON.stringify(res.data))
          // const rdata = JSON.parse(JSON.stringify(res.data))
          // const defaultData = JSON.parse(JSON.stringify(res.data))
          const sdata = JSON.parse(JSON.stringify(rdata));
          const sidebarRoutes = filterAsyncRouter(sdata);
          if (this.sidebarRouters.length <= 1) {
            this.routes = constantRoutes;
            sidebarRoutes.forEach(route => { router.addRoute(route) });
            this.addRoutes = sdata;
            this.setSidebarRouters(constantRoutes.concat(sidebarRoutes))
          }
          resolve(sidebarRoutes);
        })
      }
    }
  })
// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
  return asyncRouterMap.filter(route => {
    if (route.component) {
      if (route.component === 'Layout') {
        route.component = Layout
      } else {
        route.component = loadView(route.component);
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = filterChildren(route.children)
    } else {
      delete route['children'];
      delete route['redirect'];
    }
    return true
  })
}

function filterChildren(childrenMap) {
  var children = []
  childrenMap.forEach((el, index) => {
    el.component = loadView(el.component);
    children = children.concat(el);
  })
  return children
}


export const loadView = (view) => {
  let component;
  for (const path in modules) {
    const dir = path.split('views/')[1].split('.vue')[0];
    if (dir === view) {
      component = () => modules[path]();
    }
  }
  return component;
}

export default usePermissionStore
